<template>
<view class="container">  
  <view class="addrItems" v-for="(addr, index) in addrData" :key="index">
    <view class="addrItems_info">
      
      <image mode="aspectFit" src="https://image1.sansancloud.com/xianhua/2019_12/12/13/49/30_402.jpg?x-oss-process=style/preview_120"></image>
 
      <view class>
        <text style="font-size:30rpx;font-weight: bold" v-if="addr.community">{{addr.community}}</text>
        <text style="font-size:30rpx;font-weight: bold" v-else>{{addr.province}} {{addr.city}} {{addr.area}} {{addr.address}}</text>
        <text class="font2">{{addr.address}}</text>
        <text class="font2">{{addr.contactName}} {{addr.telNo}}</text>
      </view>
    </view>
    <view class="addrItems_deel">
      <view class="tomoren" :data-id="addr.id" @tap="setDefaultAddr">
        <block v-if="!addr.defaultAddress">
          <image mode="aspectFit" src="https://image1.sansancloud.com/xianhua/2019_12/12/15/11/25_207.jpg?x-oss-process=style/preview_120"></image> 
        </block>
        <block v-else>
          <image mode="aspectFit" :style="'background:' + setting.platformSetting.defaultColor" src="https://image1.sansancloud.com/xianhua/2019_12/12/15/11/25_829.jpg?x-oss-process=style/preview_120"></image> 
        </block>
        <text>设为默认地址</text> 
      </view>
      <view class="addrItems_deel_botton">
        <view :data-id="addr.id" @tap="writeAddr">编辑</view>
        <view :data-id="addr.id" @tap="deleteAddr">删除</view>
      </view>
    </view>
  </view>
  <view class="bottomButnHeight"></view>
  <view class="addnewAddr" @click.stop="addNewAddr" :style="'background:' + setting.platformSetting.defaultColor">
    <image src="https://image1.sansancloud.com/xianhua/2019_12/12/14/58/40_162.jpg?x-oss-process=style/preview_120"></image>
    <view>新增地址</view>
  </view>
</view>
</template>

<script>

export default {
  data() {
    return {
      setting: null,
      loginUser: null,
      addrData: null
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      setting: getApp().globalData.setting
    });
    this.setData({
      loginUser: getApp().globalData.loginUser
    });
    this.getAddr();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddr();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getAddr();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  methods: {
    /* 编辑 */
    writeAddr: function (e) {
      var addrId = e.currentTarget.dataset.id;

      for (let i = 0; i < this.addrData.length; i++) {
        if (addrId == this.addrData[i].id) {
          getApp().globalData.EditAddr = this.addrData[i];
        }
      }

      wx.navigateTo({
        url: '../add_address/index?addrId=' + addrId
      });
    },

    /* 设为默认地址 */
    setDefaultAddr: function (e) {
      var addrId = e.currentTarget.dataset.id;
      var param = {};
      param.addressId = addrId;
      var that = this;
      wx.showModal({
        title: '提示',
        content: '设为默认地址',
        success: function (res) {
          if (res.confirm) {
            // wx.showLoading({
            //   title: 'loading'
            // })
            getApp().globalData.showToastLoading('loading', true);
            var customIndex = getApp().globalData.AddClientUrl("/set_default_address.html", param, 'post');
            wx.request({
              url: customIndex.url,
              data: customIndex.params,
              header: getApp().globalData.headerPost,
              method: 'POST',
              success: function (res) {
                console.log(res);
                console.log(res.data);
                wx.hideLoading();
                that.getAddr();
              },
              fail: function (res) {
                wx.hideLoading();
                getApp().globalData.loadFail();
              }
            });
          } else if (res.cancel) {}
        }
      });
    },

    /* 删除 */
    deleteAddr: function (e) {
      var addrId = e.currentTarget.dataset.id;
      var param = {};
      param.addressId = addrId;
      param.platformNo = getApp().globalData.setting.platformSetting.platformNo;
      param.userId = getApp().globalData.loginUser.id;
      console.log(param);
      var that = this;
      wx.showModal({
        title: '提示',
        content: '确认删除？',
        success: function (res) {
          if (res.confirm) {
            // wx.showLoading({
            //   title: 'loading',
            //   mask: true
            // })
            getApp().globalData.showToastLoading('loading', true);
            var customIndex = getApp().globalData.AddClientUrl("/delete_address.html", param, 'post');
            wx.request({
              url: customIndex.url,
              data: customIndex.params,
              header: getApp().globalData.headerPost,
              method: 'POST',
              success: function (res) {
                console.log(res);
                console.log(res.data);
                wx.hideLoading();
                that.getAddr();
              },
              fail: function (res) {
                console.log(res);
                wx.hideLoading();
                getApp().globalData.loadFail();
              }
            });
          } else if (res.cancel) {}
        }
      });
    },
    addNewAddr: function () {
      wx.navigateTo({
        url: '../add_address/index'
      });
    },
    getAddr: function () {
      if (!getApp().globalData.checkIfLogin()) {
        return;
      }

      var customIndex = getApp().globalData.AddClientUrl("/get_login_user_address_list.html");
      var that = this; // wx.showLoading({
      //   title: 'loading'
      // })

      getApp().globalData.showToastLoading('loading', true); //拿custom_page 

      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log('-------地址---------');
          console.log(res.data);

          if (res.data.result.errcode == '-1') {
            console.log('err');
            getApp().globalData.echoErr(res.data.result.errMsg);
          } else {
            that.setData({
              addrData: res.data.result
            });
          }

          wx.hideLoading();
        },
        fail: function (res) {
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>